<template>
  <el-dialog :title="`${form.folderCfgId ? '修改' : '添加'}图册层级配置`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="closed">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item prop="folderCfgId" class="hide" />
      <el-form-item prop="parentId" label="上级">
        <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" show-count placeholder="选择上级" />
      </el-form-item>
      <el-form-item label="业务类型编码" prop="folderCfgCode"><el-input v-model.trim="form.folderCfgCode" placeholder="文件夹业务类型编码" :maxlength="50" /></el-form-item>
      <el-form-item label="文件夹名称" prop="folderCfgName"><el-input v-model.trim="form.folderCfgName" placeholder="文件夹业务类型名称" :maxlength="255" /></el-form-item>
      <el-form-item label="排序" prop="seq"><el-input-number v-model="form.seq" :min="0" :max="1e8" :precision="0" controls-position="right" /></el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改图册层级配置
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { nameRule } from '@/utils/validate'
import { editFormMixin } from '@/utils/mixin'
import { addProFolderCfg, setProFolderCfg } from '@/api/baseManage/projectAlbum'
export default {
  components: { Treeselect },
  mixins: [editFormMixin],
  data() {
    return {
      primaryKey: 'folderCfgId',
      addApi: addProFolderCfg,
      updateApi: setProFolderCfg,
      form: {
        folderCfgId: '',
        parentId: '',
        folderCfgCode: '',
        folderCfgName: '',
        seq: 0
      },
      rules: {
        folderCfgCode: [{ required: true, message: '请输入文件夹业务类型编码' }, nameRule],
        folderCfgName: [{ required: true, message: '请输入文件夹业务类型名称' }, nameRule]
      },
      treeOptions: []
    }
  },
  methods: {
    closed() {
      this.$refs.form.resetFields()
      this.treeOptions = []
    },
    opened(item, { treeData }) {
      this.treeOptions = [{ folderCfgId: '', folderCfgName: '主类目', children: treeData }]
    },
    normalizer(node) {
      if (!node.children?.length) delete node.children
      return { id: node.folderCfgId, label: node.folderCfgName, children: node.children }
    }
  }
}
</script>
